<@pp.dropOutputFile/>
<#include "../common/util.ftl">
<#-- iterate table list-->
<#list doc.tables as table>

<#-- generate package name -->
<#assign app = doc.artifactid?replace(".", "/")>
<#assign package = table.moduleid?replace(".", "/")>
<#assign fileName = table.name?cap_first>
<@pp.changeOutputFile name = "/" + package  + "/View/" + fileName + "Form.vb"/>
<#assign package = table.moduleid?replace("/", ".")>
Imports System.Windows.Forms
Imports DevComponents.DotNetBar.SuperGrid
Imports DevComponents.DotNetBar
Imports Stone.common
Imports Stone.loading
Imports ${app}.${package}
Imports Stone

Public Class ${fileName}Form
    Private _${fileName}Service As I${fileName}Service = Nothing
    Private _Log As log4net.ILog = log4net.LogManager.GetLogger(Reflection.MethodBase.GetCurrentMethod().DeclaringType)
	Public Property DB As IDbConnection

    Private Sub ${fileName}Form_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        _${fileName}Service = New ${fileName}ServiceSqliteImpl(_DB)
		'_${fileName}Service = New ${fileName}ServiceSqliteImpl()
        'ComboBoxItem_PageSize.Text = _Size
    End Sub

    ''' <summary>
    ''' 查询参数
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Property QueryParameter As Dictionary(Of String, Object)
        Get
            Dim id As String = TextBoxX_Query_Id.Text
            Dim params As Dictionary(Of String, Object) = New Dictionary(Of String, Object)
            If id.Length > 0 Then
                params.Add("id", id)
            End If

            Return params
        End Get

        Set(value As Dictionary(Of String, Object))

        End Set
    End Property

    ''' <summary>
    ''' 查询
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub ButtonX_Query_Click(sender As System.Object, e As System.EventArgs) Handles ButtonX_Query.Click
        Paginator1.Count = _${fileName}Service.size(QueryParameter).Data
    End Sub

    ''' <summary>
    ''' 翻页
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="args"></param>
    ''' <remarks></remarks>
    Private Sub Paginator1_PageChanged(sender As System.Object, args As PageChangedEventArgs) Handles Paginator1.PageChanged
        Dim loader As Loader = loader.getInstance()
        loader.Loading(SqliteDaoSupport.getText("load.page"))
        Dim objs As List(Of ${package}.${fileName}) = _${fileName}Service.getPage(args.Page, args.Size, QueryParameter).Data
        SuperGridControl_Data.PrimaryGrid.DataSource = ${fileName}.toListString(objs)
        loader.Loaded()
    End Sub

    ''' <summary>
    ''' 将当前条件筛选的全部记录导出至EXCEL（最多5000条）
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="args"></param>
    ''' <remarks></remarks>
    Private Sub Paginator1_ExportAll(sender As System.Object, args As PageChangedEventArgs) Handles Paginator1.ExportAll
        Dim odf As OpenFileDialog = New OpenFileDialog
        odf.Filter = "Excel 2003(*.xls)|*.xls"
        odf.CheckPathExists = False
        odf.CheckFileExists = False
        If (odf.ShowDialog() = Windows.Forms.DialogResult.OK) Then
            Dim loader As Loader = loader.getInstance()
            loader.Loading(SqliteDaoSupport.getText("export.all"))
            Dim objs As List(Of ${package}.${fileName}) = _${fileName}Service.getPage(0, 5000, QueryParameter).Data
            _${fileName}Service.toExcel(objs, odf.FileName)
            loader.Loaded()
        End If
    End Sub

    ''' <summary>
    ''' 将当前页导出至EXCEL
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="args"></param>
    ''' <remarks></remarks>
    Private Sub Paginator1_ExportOne(sender As System.Object, args As PageChangedEventArgs) Handles Paginator1.ExportOne
        Dim odf As OpenFileDialog = New OpenFileDialog
        odf.Filter = "Excel 2003(*.xls)|*.xls"
        odf.CheckPathExists = False
        odf.CheckFileExists = False
        If (odf.ShowDialog() = Windows.Forms.DialogResult.OK) Then
            Dim loader As Loader = loader.getInstance()
            loader.Loading(SqliteDaoSupport.getText("export.one"))
            Dim objs As List(Of ${package}.${fileName}) = _${fileName}Service.getPage(args.Page, args.Size, QueryParameter).Data
            _${fileName}Service.toExcel(objs, odf.FileName)
            loader.Loaded()
        End If
    End Sub

    Private _ActivedRow As GridRow = Nothing

    Private Sub SuperGridControl_Data_RowActivated(sender As System.Object, e As DevComponents.DotNetBar.SuperGrid.GridRowActivatedEventArgs) Handles SuperGridControl_Data.RowActivated
        _ActivedRow = e.NewActiveRow
        If (Not IsNothing(_ActivedRow)) Then
            Dim obj_string As ${app}.${package}.${fileName}String = _ActivedRow.DataItem
			<#list table.fields as field>
			<#if isText(field.type)>
			TextBoxX_${field.name?cap_first}.Text = obj_string.${field.name?cap_first}
			</#if>
			<#if isInteger(field.type)>
			IntegerInput_${field.name?cap_first}.Text = obj_string.${field.name?cap_first}
			</#if>
			<#if isReal(field.type)>
			DoubleInput_${field.name?cap_first}.Text = obj_string.${field.name?cap_first}
			</#if>
			<#if isDate(field.type)>
			DateTimeInput_${field.name?cap_first}.Text = obj_string.${field.name?cap_first}
			</#if>
			</#list>
        Else
		<#list table.fields as field>
		<#if isText(field.type)>
        TextBoxX_${field.name?cap_first}.Text = ""
		</#if>
		<#if isInteger(field.type)>
        IntegerInput_${field.name?cap_first}.Text = ""
		</#if>
		<#if isReal(field.type)>
        DoubleInput_${field.name?cap_first}.Text = ""
		</#if>
		<#if isDate(field.type)>
        DateTimeInput_${field.name?cap_first}.Text = ""
		</#if>
		</#list>
        End If
    End Sub

    ''' <summary>
    ''' 修改
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub ButtonX_Update_Click(sender As System.Object, e As System.EventArgs) Handles ButtonX_Update.Click
        Dim loader As Loader = loader.getInstance()
        loader.Loading(SqliteDaoSupport.getText("update"))
		If (Not IsNothing(_ActivedRow)) Then
			Dim obj_string As ${package}.${fileName}String = _ActivedRow.DataItem
            Dim obj As ${package}.${fileName} = obj_string.Obj
			<#list table.fields as field>
			<#if isText(field.type)>
			obj.${field.name?cap_first} = TextBoxX_${field.name?cap_first}.Text
			</#if>
			<#if isInteger(field.type)>
			obj.${field.name?cap_first} = IIf(IntegerInput_${field.name?cap_first}.IsEmpty, Nothing, IntegerInput_${field.name?cap_first}.Value)
			</#if>
			<#if isReal(field.type)>
			obj.${field.name?cap_first} = IIf(DoubleInput_${field.name?cap_first}.IsEmpty, Nothing, DoubleInput_${field.name?cap_first}.Value)
			</#if>
			<#if isDate(field.type)>
			obj.${field.name?cap_first} = IIf(DateTimeInput_${field.name?cap_first}.IsEmpty, Nothing, DateTimeInput_${field.name?cap_first}.Value)
			</#if>
			</#list>
			If (Not IsNothing(_ActivedRow) And Not IsNothing(obj)) Then
				Dim oc As Outcome = _${fileName}Service.update(obj)
				If (oc.Success) Then
					Dim objs As List(Of ${app}.${package}.${fileName}) = _${fileName}Service.getPage(Paginator1.Page, Paginator1.PageSize, QueryParameter).Data
					SuperGridControl_Data.PrimaryGrid.DataSource = ${fileName}.toListString(objs)
				Else
					_Log.Error(oc.Detail)
					ToastNotification.Show(Me, oc.Detail)
				End If
			End If
		End If
        loader.Loaded()
    End Sub

    ''' <summary>
    ''' 删除
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub ButtonX_Delete_Click(sender As System.Object, e As System.EventArgs) Handles ButtonX_Delete.Click
        Dim loader As Loader = loader.getInstance()
        loader.Loading(SqliteDaoSupport.getText("delete"))
        Dim rows = SuperGridControl_Data.PrimaryGrid.SelectedRows
        Dim ids = New List(Of String)

        For Each row As GridRow In rows
           Dim obj_string As ${app}.${package}.${fileName}String = _ActivedRow.DataItem
           Dim obj As ${app}.${package}.${fileName} = obj_string.Obj
            ids.Add(obj.Id)
        Next
        Dim oc As Outcome = _${fileName}Service.delete(ids)
        If (oc.Success) Then
            _Log.Debug(oc.Message)
        Else
            _Log.Error(oc.Detail)
			ToastNotification.Show(Me, oc.Detail)
        End If
        Paginator1.Count = _${fileName}Service.size(QueryParameter).Data
        loader.Loaded()
    End Sub

    ''' <summary>
    ''' 新增
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub ButtonX_New_Click(sender As System.Object, e As System.EventArgs) Handles ButtonX_New.Click
        Dim loader As Loader = loader.getInstance()
        loader.Loading(SqliteDaoSupport.getText("new"))
        Dim obj As ${package}.${fileName} = New ${package}.${fileName}()
		<#list table.fields as field>
		<#if field.name != "id">
		<#if isText(field.type)>
        obj.${field.name?cap_first} = TextBoxX_${field.name?cap_first}_New.Text
		</#if>
		<#if isInteger(field.type)>
        If (Not IntegerInput_${field.name?cap_first}_New.IsEmpty) Then
            obj.${field.name?cap_first} = IntegerInput_${field.name?cap_first}_New.Value
        End If
		</#if>
		<#if isReal(field.type)>
        If (Not DoubleInput_${field.name?cap_first}_New.IsEmpty) Then
            obj.${field.name?cap_first} = DoubleInput_${field.name?cap_first}_New.Value
        End If
		</#if>
		<#if isDate(field.type)>
        If (Not DateTimeInput_${field.name?cap_first}_New.IsEmpty) Then
            obj.${field.name?cap_first} = DateTimeInput_${field.name?cap_first}_New.Value
        End If
		</#if>
		</#if>
		</#list>

        If (Not IsNothing(obj)) Then
            Dim oc As Outcome = _${fileName}Service.create(obj)
            If (oc.Success) Then
                Paginator1.Count = _${fileName}Service.size(QueryParameter).Data
            Else
                _Log.Error(oc.Detail)
				ToastNotification.Show(Me, oc.Detail)
            End If
        End If
        loader.Loaded()
    End Sub

    Private Sub SuperGridControl_Data_GetCellFormattedValue(sender As System.Object, e As DevComponents.DotNetBar.SuperGrid.GridGetCellFormattedValueEventArgs) Handles SuperGridControl_Data.GetCellFormattedValue
        If (IsNothing(e.FormattedValue)) Then
            e.FormattedValue = ""
        End If

    End Sub
	
	Private Sub ButtonX_Report_Click(sender As System.Object, e As System.EventArgs) Handles ButtonX_Report.Click
		If (Not IsNothing(_ActivedRow)) Then
            Dim obj_string As ${package}.${fileName}String = _ActivedRow.DataItem

            If (Not IsNothing(obj_string)) Then
                Dim bt As ${package}.${fileName} = obj_string.Obj
                'LoadParams(bt)
                _${fileName}Service.loadBuddies(bt)

                Dim form As ${fileName}ReportForm = New ${fileName}ReportForm()
                form.Show()
            End If
        End If
    End Sub
End Class
</#list>